<?php

/**
 * 文件后台Controller
 */
namespace Files\Controller;
use Common\Controller\AdminbaseController;

class FileManageradminController extends AdminbaseController
{

	private $files_model,$file_id;

	public function _initialize()
	{
		$this->files_model=D("Files/Files");
		$this->file_id=$this->files_model->getPk();
		parent::_initialize();
	}
	// 文件管理首页界面
	public function index(){
		//查询所有文件类型
		$types=$this->files_model->getFileTypes();
		$type=strval(I("fileType"));
		$type=strtolower($type);
		(!in_array($type,$types)||empty($types)) && $type="all";
        $type!=='all' && $where=array('file_category'=>$type);
        empty($where) && $where=array();

		$where['status']=1;
		$where['parent_id']=0;
        $count=$this->files_model->where($where)->count();
        $page=$this->page($count,12);
        $lists=$this->files_model->where($where)
               ->order(" create_time desc ")
               ->limit($page->firstRow,$page->listRows)
               ->select();
        /*自定义后台的分页样式*/
		$this->assign("pages",$page->show("tciocEx"));
		$this->assign("types",$types);
		$this->assign("lists",$lists);
		$this->display("index");
	}
	//不是真正的删除
	public function delete(){
		$id=I("get.id/d",0);
		if($id<=0){
			$this->error(L("FILE_DELETE_FAIL"),"",true);
		}
		$this->files_model->where()->save();
		$pk=$this->file_id;
		$where=array($pk=>$id);
		$data=array('status'=>0);
		if($this->saveFile($where,$data)===false){
			$this->error(L("FILE_DELETE_FAIL"),"",true);
		}
		$this->success(L("FILE_DELETE_SUCCESS"),"",true);
	}

	// 保存文件更改的信息
	public function save(){
		$id=I("post.id/d",0);
		if($id<=0){
			$this->error(L("ARGS_ERROR"),"",true);
		}
		$where=array($this->file_id=>$id);
		$data=array(
			'name'=>I("post.name/s"),
			'title'=>I("post.title/s"),
			'desc'=>I("post.desc/s"),
			'uri'=>I("post.uri/s"),
		);
		if($this->saveFile($where,$data)===false){
			$this->error(L("HANDLE_ERROR"),"",true);
		}
		$this->success(L("HANDLE_SUCCESS"),"",true);
	}

	/**
	 * 保存数据的更改
	 * @param array $where  查询条件
	 * @param array $data 保存数据
	 * @return mixed
	 */
	private function saveFile($where,$data){
		return $this->files_model->where($where)->save($data);
	}

}